#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_N 1005
#define K 105
int dp_buy_status[MAX_N][K];  // 买入状态
int dp_sell_status[MAX_N][K]; // 卖出状态
int prices[MAX_N];

int main()
{
    int k, n;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> prices[i];
    }
    for(int i =0; i <= k; i++) 
    {
        dp_buy_status[0][i] = -0x3f3f3f3f;
        dp_sell_status[0][i] = -0x3f3f3f3f;
    }
    dp_buy_status[0][0] = -prices[0];
    dp_sell_status[0][0] = 0;
    int ret = 0;
    for (int i = 1; i < n; i++)
    {
        for (int j = 0; j <= k; j++)
        {
            
            dp_buy_status[i][j] = max(dp_buy_status[i - 1][j], dp_sell_status[i - 1][j] - prices[i]);
            // if(j == 0) continue;
            //  dp_sell_status[i][j] = max(dp_sell_status[i - 1][j], dp_buy_status[i - 1][j - 1] + prices[i]);
            dp_sell_status[i][j] = dp_sell_status[i - 1][j];
            if(j >= 1)
            {
                dp_sell_status[i][j] = max(dp_sell_status[i][j], dp_buy_status[i- 1][j - 1] + prices[i]);
            }
            
        }
    }
    for(int i = 0;i <= k; i++) ret = max(dp_sell_status[n - 1][i], ret);
    std::cout << ret;
    return 0;
}